/**
 * Created by Noir on 2/6/14.
 */
'use strict'
var Chart = require("../../models/chart"),
    ChartDetail = require('../../models/chartDetail'),
    helper = require("../../../helper/helper"),
    userValidator = require("../../../helper/userValidator.js");

exports.createNewChart = function(req, res, next){
    // check user is exist
    userValidator.checkUserValid(req.session.passport.user.id, function(err, creatorId){
        if(err){
            return res.send({error:err});
        }
        if(!creatorId){
            // acount is locked
            return res.send({error: 'Your account has been deleted or locked. Please try again.'});
        }
        // this user has right to create a new chart
        var newChart = new Chart();
        // data input
        newChart.name = req.body.name;
        newChart.caption = req.body.caption;
        newChart.description = req.body.description;
        newChart.type = req.body.type;
        newChart.creator = creatorId;
        // save
        newChart.save(function(err, chart){
            if(err) {
                var errorMessage = helper.displayMongooseError(err);
                return res.send(500,{error:errorMessage});
            }

            return res.send({id:chart._id});
        });

    });
}

exports.createNewChartDetail = function(req, res, next){
    // get candidate's type
    var detailType = req.body.type;
    var detail = new ChartDetail();

    userValidator.checkUserValid(req.session.passport.user.id, function(err, creatorId){
        if(err){
            return res.send({error:err});
        }
        if(!creatorId){
            // acount is locked
            return res.send({error: 'Your account has been deleted or locked. Please try again.'});
        }
        // initialize
        detail.adder = creatorId;
        detail.position = req.body.position;
        detail.content = {};
        switch(detailType){
            // candidate is text
            case 'text':
                detail.content.title = req.body.title;
                detail.content.paragraph = req.body.paragraph;
                detail.type = 'text';
                break;
        }
        // save detail
        detail.save(function(err, detail){
            if(err){
                var errorMessage = helper.displayMongooseError(err);
                return res.send(500,{error:errorMessage});
            }

            // return candidate's id
            return res.send(200, {id:detail._id});
        });
    });
}

/**
 * retrieve all candidates of the chart
 *
 * @param req
 * @param res
 * @param next
 */
//exports.findAllDetails = function(req, res, next){
//    var chartId = req.params.chartId;
//
//    Chart.findOne({'_id':chartId},function(err, chart){
//        if(err){
//            return res.send(500,{error:err});
//        }
//
//        if(chart){
//            var items = [];
//            if(chart.item.length>0){
//                for(var i=0;i<chart.item.length;i++){
//
//                }
//            }
//
//            return res.send({'items':chart.item});
//        }else{
//            // not found
//            return res.send(400, {error:"This chart doesn't exist"});
//        }
//    });
//}

exports.addDetailToChart = function(req, res, next){
    var chartId = req.params.chartId;
    var detailId = req.body.detailId;

    userValidator.checkUserValid(req.session.passport.user.id, function(err, creatorId){
        if(err){
            return res.send(500,{error:err});
        }
        if(!creatorId){
            // acount is locked
            return res.send({error: 'Your account has been deleted or locked. Please try again.'});
        }

        // get detail
        ChartDetail.findOne({'_id':req.body.detailId},function(err, detail){
            if(err){
                return res.send(500,{error:err});
            }

            if(detail){
                // add chart detail into chart
                Chart.update({'_id':chartId},{$push:{'item':detail._id}},function(err, chart){
                    if(err){
                        return res.send(500, {error: 'Something wrong just happened. Please try again.'});
                    }

                    // updated successfully
                    return res.send(200);
                });
            }else{
                return res.send(400, 'Detail is no longer exist.');
            }
        })
    });
}

exports.findOneChartDetail = function(req, res, next){
    var chartDetailId = req.params.id;

    // find it now
    ChartDetail.findOne({'_id':chartDetailId},function(err, chartDetail){
        if(err){
            return res.send(500, {error: 'Something wrong just happened. Please try again.'});
        }

        if(chartDetail){
            return res.send(200, chartDetail);
        }else{
            return res.send(200);
        }
    });
}